package com.yanqu.road.server.cmd.activity.busyfarming;


import com.yanqu.road.entity.activity.busyfarming.BusyFarmingUserData;
import com.yanqu.road.pb.activity.BusyFarmingProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.busyfarming.BusyFarmingModule;
import com.yanqu.road.server.manger.activity.busyfarming.BusyFarmingMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.S_BUSY_FARMING_STEAL_SEARCH_FROM_CROSS,desc = "偷菜查找-跨服返回")
public class BusyFarmingStealSearchCmdFromCross extends AbstractCommand {

    int activityId;
    int crossRet;
    List<BusyFarmingProto.CrossBusyFarmingStealDataTemp> list = new ArrayList<>();
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BusyFarmingProto.CrossBusyFarmingStealSearchRespMsg respMsg = BusyFarmingProto.CrossBusyFarmingStealSearchRespMsg.parseFrom(packet.getBytes());
        crossRet = respMsg.getRet();
        activityId = respMsg.getActivityId();
        if(!respMsg.getStealList().isEmpty()){
            list.addAll(respMsg.getStealList());
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        BusyFarmingUserData userData = BusyFarmingMgr.getBusyFarmingUserData(player.getUserId());
        if(userData == null){
            ret = GameErrorCode.E_BUSY_FARMING_ACTIVITY_NO_UNLOCK;
            return;
        }
        ret = player.getModule(BusyFarmingModule.class).initStealCacheData(userData, list, activityId);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            BusyFarmingProto.CrossBusyFarmingStealSearchRespMsg.Builder respMsg = BusyFarmingProto.CrossBusyFarmingStealSearchRespMsg.newBuilder();
            respMsg.setRet(ret);
            respMsg.setCsrf(0);
            respMsg.setActivityId(activityId);
            player.sendPacket(Protocol.U_BUSY_FARMING_STEAL_SEARCH, respMsg);
        }
    }
}
